Repository navigation
Fix TopicPartition metadata over-read when metadata isn't NUL-terminated - #2431
Open
Steven Yuen (steveny91) wants to merge 1 commit into
Open
Steven Yuen (steveny91) wants to merge 1 commit into
Steven Yuen (steveny91) wants to merge 1 commit into
Conversation
c_part_to_py() dropped rktpar->metadata_size and TopicPartition_setup() strdup()'d the metadata, but librdkafka returns it as a sized buffer with no guaranteed NUL terminator. Offsets committed by clients that store the exact length (kafka-python, Java) were read past the end of the buffer. Thread metadata_size through TopicPartition_new0() into TopicPartition_setup() and copy exactly that many bytes plus a NUL. Fixes confluentinc#2430.
|
🎉 All Contributor License Agreements have been signed. Ready to merge. |
Steven Yuen (steveny91)
marked this pull request as ready for review
November 13, 2026 22:28
Steven Yuen (steveny91)
requested review from
a team and
Matthew Seal (MSeal)
as code owners
November 13, 2026 22:28
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes #2430.
c_part_to_py()passedrktpar->metadatatoTopicPartition_new0()withoutrktpar->metadata_size, andTopicPartition_setup()copied it withstrdup(). librdkafka returns metadata as a sized buffer with no guaranteed NUL, so for offsets committed by clients that don't store a terminator (kafka-python, Java, ...)strdup()read past the end of the buffer. Depending on the heap,.metadatacame back with trailing garbage, raisedUnicodeDecodeError, or the process crashed with SIGSEGV on librdkafka's background thread.This change threads
metadata_sizethroughTopicPartition_new0()intoTopicPartition_setup(), which now copies exactlymetadata_sizebytes and appends a NUL.TopicPartition_init()(Python-constructed partitions) passesstrlen(metadata), so its behaviour is unchanged.metadata_size, so their copy ends in two NULs and.metadatais unchanged.malloc+memcpyrather thanstrndup(), becausestrndup()isn't available on MSVC.c_part_to_py()is the only C-side reader of partition metadata, so this covers every API that returnsTopicPartitions (Consumer.committed(),AdminClient.list_consumer_group_offsets(), ...).Checklist
TopicPartition.metadatanow contains exactly the committed bytes. No API change.test_consumer_committed_metadata_without_nul_terminator_returns_committed_bytesintests/integration/consumer/test_consumer_topicpartition_metadata.py.References
JIRA: n/a (external contributor)
rd_kafka_topic_partition_t.metadata/metadata_sizeinrdkafka.h(no NUL promised).Test & Review
The new test commits 248 bytes of metadata on 8 partitions with a hand-built OffsetCommit v2 request (confluent-kafka itself always stores the NUL, so it can't produce this case), then reads it back with
Consumer.committed()and checks the exact value. 248 bytes makes the failure show on every run under glibc: the allocation's usable size then ends exactly at the next chunk's header, so the over-read picks up non-zero bytes after (nearly) every buffer.Run against a single
apache/kafka:4.2.0broker (BROKERS=localhost:9092), master built against librdkafka 2.16.0, Ubuntu 24.04 aarch64:Without the fix: fails 10 of 10 runs, at the assertion:
Position 248 is the first byte past the committed metadata.
With the fix: passes 10 of 10 runs, together with the existing
test_consumer_topicpartition_metadata.Unit tests (
tests/withouttests/integrationandtests/soak): the same tests fail with and without this change in my environment (no AWS credentials, timing-sensitive close-race and memory-safety tests).tests/integration/consumer+tests/integration/admin: the same 3 tests fail on master and with this change (no ACL authorizer, broker config defaults, a timing test), plus one setup error from the trivup SASL cluster.Under jemalloc, with this change, every partition returns exactly the committed bytes: 256 bytes × 2,048 partitions through
list_consumer_group_offsets()(3 of 3 runs; the shape that crashed in production) and 248 bytes × 2,048 throughcommitted()(2 of 2).AddressSanitizer, same 256 × 2,048
list_consumer_group_offsets()read: master reportsheap-buffer-overflow ... 0 bytes after 256-byte regioninstrdup←TopicPartition_setup←c_part_to_py←c_parts_to_py←Admin_background_event_cb; with this change, no report and all partitions correct.flake8andblack --checkpass on the test file;clang-formatreports nothing in the changed C lines.The same C change, as a patch on 2.13.2, has also run in production on the workload that originally crashed: the over-read crash stopped (previously within minutes, then none in ~16 hours).
Open questions / Follow-ups
struct.pack). Adding kafka-python as a test dependency would be shorter; happy to switch if you'd prefer that.mallocis unchecked, matching the existingstrdup()calls in this function. Happy to add a check here.